Component({
  data: {
    selected: 0,
    isLoggedIn: false,
    list: [{
      pagePath: "/pages/index/index",
      text: "首页"
    }, {
      pagePath: "/pages/menu/menu",
      text: "菜单"
    }, {
      pagePath: "/pages/cart/cart",
      text: "购物车"
    }, {
      pagePath: "/pages/user/user",
      text: "我的"
    }]
  },

  lifetimes: {
    attached() {
      this.checkLoginStatus();
    }
  },

  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset;
      const url = this.data.list[data.index].pagePath;
      wx.switchTab({
        url
      });
      this.setData({
        selected: data.index
      });
    },

    checkLoginStatus() {
      const token = wx.getStorageSync('token');
      this.setData({
        isLoggedIn: !!token
      });
    },

    goToLogin() {
      wx.navigateTo({
        url: '/pages/login/login'
      });
    }
  }
}); 